import axios from 'axios';
import qs from 'qs';
import baseurl from './baseurl'
// eslint-disable-next-line no-unused-vars

const instance = axios.create({
    baseURL: baseurl,
    timeout: 10000,
})

instance.defaults.responseType = 'json'

instance.defaults.transformRequest = [
    data => {
        return qs.stringify(data)
    }
]

instance.interceptors.request.use(//请求拦截器，在发送请求前do something
    config => {
        const token = localStorage.getItem('token')
        if (token) { // 判断是否存在token，如果存在的话，则每个http header都加上token
            config.headers.token = token  //请求头加上token
        }
        return config
    },
    error => {
        console.log("请求超时")
        return Promise.reject(error)
    }
)

instance.interceptors.response.use(
    response => {
        const res = response.data;
        console.log(res);
        return res
    },
    error => {
        console.log(error.data);
        return Promise.reject(error)
    }
)

export default instance